﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Controls;

namespace WFClient
{
    public partial class Form2 : Form
    {
        private string urlstr { set; get; }
        public Form2(string str)
        {
            urlstr = str;
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            //QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            //QrCode qrCode = new QrCode();
            //qrEncoder.TryEncode(urlstr, out qrCode);

            //Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
            //renderer.CreateImageFile(qrCode.Matrix, "aa.png", ImageFormat.Png);
            this.pictureBox1.Image = streamsToImage();
        }

        public Image streamsToImage()
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = new QrCode();
            qrEncoder.TryEncode(urlstr, out qrCode);

            Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
            MemoryStream ms = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.Png);
            //MemoryStream ms = new MemoryStream(Bytes);
            return Bitmap.FromStream(ms, true);
            //return ms;
        }
        public void Graphics()
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = qrEncoder.Encode(urlstr);

            const int moduleSizeInPixels = 5;
            Renderer renderer = new Renderer(moduleSizeInPixels, Brushes.Black, Brushes.White);

            Panel panel = new Panel();
            Point padding = new Point(10, 16);
            Size qrCodeSize = renderer.Measure(qrCode.Matrix.Width);
            panel.AutoSize = false;
            panel.Size = qrCodeSize + new Size(2 * padding.X, 2 * padding.Y);

            using (Graphics graphics = panel.CreateGraphics())
            {
                renderer.Draw(graphics, qrCode.Matrix, padding);
            }
        }
        //private void writeablebitmap()
        //{
        //    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
        //    QrCode qrCode = new QrCode();
        //    qrEncoder.TryEncode(urlstr, out qrCode);

        //    const int moduleSizeInPixels = 5;
        //    Renderer renderer = new Renderer(moduleSizeInPixels);   //Black&White is default colour for drawing QrCode

        //    //Matrix under qrCode might be null if input string is null or empty. 21 module wide is version 1 QrCode's width. 
        //    int pixelSize = qrCode.Matrix == null ? renderer.Measure(21) : renderer.Measure(qrCode.Matrix.Width);

        //    WriteableBitmap wBitmap = new WriteableBitmap(pixelSize, pixelSize, 96, 96, PixelFormats.Gray8, null);

        //    //If wBitmap is null value. renderer will create Gray8 Bitmap as default.
        //    renderer.Draw(wBitmap, qrCode.Matrix);    //Default offset position is (0, 0);

        //    //Now you can put wBitmap to Image control's Source or use it to create image file. 
        //}
    }
}
